﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace CommonUtils
{
    public class GitLog
    {
        public string Version;
        public string Author;
        public string AuthorEmail;
        public DateTime DateTime;
        public string Content;

        /*
commit 387442974400a4ed50d9bb0ae09daf0a1174fed1
Author: 蒋龙 <372792797@qq.com>
Date:   Wed Sep 29 11:38:19 2021 +0800

审批办理流程

commit a809b1f0f6a22180bc9cf5d5c3d381ae9dd3d143
Author: 蒋龙 <372792797@qq.com>
Date:   Wed Sep 29 10:03:45 2021 +0800

添加注释

commit d6b80953f73ddbec8726c1cacea89d4cf06307f5
Author: 张世宇 <3276054029@qq.com>
Date:   Wed Sep 29 10:02:41 2021 +0800

出国申报审核经办页面

commit 6339b7808582cb0551ea46f70e1f0a9f82cef792
Author: 蒋龙 <372792797@qq.com>
Date:   Wed Sep 29 09:32:09 2021 +0800

添加新表

commit d3a6cecc99f46ef01da855a397b9f549aa5c3b9c
Author: 蒋龙 <372792797@qq.com>
Date:   Wed Sep 29 09:31:46 2021 +0800

字段变更

*/
        public static GitLog[] Analysis(string output)
        {
            var items = new List<GitLog>();
            var dailyPaper = new StringBuilder();
            var logs = output.Trim().SplitAndRemoveEmpty("commit ");
            foreach (var log in logs)
            {
                var lines = log.GetLines();
                if (lines.Length < 4)
                    continue;
                var item = new GitLog();
                item.Version = lines[0].Remove("commit ");
                item.Author = lines[1].GetMiddleStrings("Author: ", " <").FirstOrDefault();
                item.AuthorEmail = lines[1].GetMiddleStrings("<", ">").FirstOrDefault();
                item.DateTime = TimeUtil.ParseExact(lines[2].Remove("Date:   "));
                item.Content = lines.Take(4, lines.Length - 4).JoinToText().Trim();
                if (item.Content.Contains("Merge"))
                    continue;
                items.Add(item);
                dailyPaper.InsertLine();
                dailyPaper.InsertLine("{} 在 {} 完成了\r\n{}", item.Author, item.DateTime.Info(), item.Content);
            }
            dailyPaper.Print();
            return items.ToArray();
        }
    }
}
